<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends CommonController{
    
    public function lst(){
        $cate = D('cate');
        $res = $cate->order('sort asc')->select();
        $this->assign('cate',$res);
        $this->display();
    }
    
    public function add(){
        $cate = D('cate');
        if(IS_POST){
            $data['catename'] = I('catename');
            $data['sort'] = I('sort');
            /*
            if($cate->add($data)){
                $this->success('添加成功',U('lst'));
            }else{
                $this->error('添加失败');
            }*/
            if ($cate->create($data)){
                if($cate->add()){
                    $this->success('添加成功',U('lst'));
                }else{
                    $this->error('添加失败');
                }
            }else {
                $this->error($cate->getError());
            }
            return ;
        }
        $this->display();
    }
    
    public function edit(){
        $cate = D('cate');
        if(IS_POST){
            $data['id'] = I('id');
            $data['catename'] = I('catename');
            $data['sort'] = I('sort');
            if($cate->create($data)){
                $save=$cate->save();
                if( $save !== false){
                    $this->success('修改栏目成功！',U('lst'));
                }else{
                    $this->error('修改栏目失败！');
                }
            }else{
                $this->error($cate->getError());
            }
            return;
        }
        $res = $cate->find(I('id'));
        $this->assign('cates',$res);
        $this->display();
    }
    
    public function del(){
        $cate = D('cate');
        if($cate->delete(I('id'))){
            $this->success('删除成功',U('lst'));
        }else{
            $this->error('删除失败');
        }
    }
    
    public function sort(){
        $cate = D('cate');
        foreach ($_POST as $id =>$sort){
            $cate->where(array('id'=>$id))->setField('sort',$sort);
        }
        $this->success('排序成功',U('lst'));
    }
    
}